<template>
	<div class="evidence-unit-evidenceInConfirm-container">
		<el-card shadow="hover" body-style="padding:8px; ">
			<el-row justify="end" style="margin-bottom: 15px; margin-top: 3px">
				<el-col :span="24">
					<div style="float: left; height: 40px; width: 5px; background: green"></div>
					<div style="float: left; margin-top: 10px; margin-left: 20px; font-size: 16px">入库清单 {{ deliveryNo }}</div>
				</el-col>
			</el-row>
			<el-table
				ref="multipleTable"
				:data="tableData"
				:default-sort="{ prop: 'name', order: 'descending' }"
				:row-style="{ height: '40px' }"
				:cell-style="{ padding: '1' }"
				size="medium"
				style="width: 100%"
			>
				<el-table-column align="center" label="序号" type="index" width="60"> </el-table-column>
				<el-table-column align="center" property="no" sortable label="物品编号" show-overflow-tooltip />
				<el-table-column align="center" sortable label="物品名称/数量" show-overflow-tooltip>
					<template #default="scope">{{ `${scope.row.name}/${scope.row.quantity}` }}</template>
				</el-table-column>
				<el-table-column align="center" property="source" sortable label="规格型号" show-overflow-tooltip />
				<el-table-column align="center" property="extractionParts" sortable label="产地" show-overflow-tooltip />
				<el-table-column align="center" property="evidencePerson" sortable label="颜色/获取时间" show-overflow-tooltip>
					<template #default="scope">{{ `${scope.row.extractionPersonName}/${dateFormat(scope.row.extractionDateTime)}` }}</template>
				</el-table-column>
				<el-table-column align="center" property="extractMethod" sortable label="获取方式" show-overflow-tooltip />
				<el-table-column align="center" property="status" sortable label=" 物品状态" show-overflow-tooltip>
					<template #default="scope">{{ `${getEvidenceStatus(scope.row.status) ?? '未知'}` }}</template>
				</el-table-column>
			</el-table>
			<el-row justify="end" style="margin-bottom: 15px; margin-top: 10px">
				<el-col :span="24">
					<div style="float: left; height: 40px; width: 5px; background: green"></div>
					<div style="float: left; margin-top: 10px; margin-left: 20px; font-size: 16px">入库交接信息</div>
				</el-col>
			</el-row>
			<el-form :inline="true" size="mini" label-width="100px">
				<el-row>
					<el-col :span="8" class="mb10">
						<el-form-item label="移交单位：" style="margin-bottom: 0px"> {{ formInline.transferorOrgan }} </el-form-item>
					</el-col>
					<el-col :span="8" class="mb10">
						<el-form-item label="移交人：" style="margin-bottom: 0px"> {{ formInline.transferor }} </el-form-item>
					</el-col>
					<el-col :span="8" class="mb10">
						<el-form-item label="移交时间：" style="margin-bottom: 0px"> {{ dateFormatTime(formInline.deliveryTime) }} </el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="8" class="mb10">
						<el-form-item label="移交电话：" style="margin-bottom: 0px"> {{ formInline.transferorPhone }} </el-form-item>
					</el-col>
					<el-col :span="8" class="mb10">
						<el-form-item label="存放位置：" style="margin-bottom: 0px"> {{ formInline.location }} </el-form-item>
					</el-col>
					<el-col :span="8" class="mb10">
						<el-form-item label="保存期限：" style="margin-bottom: 0px"> {{ dateFormatTime(formInline.returnTime) }} </el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="8" class="mb10">
						<el-form-item label="保管单位：" style="margin-bottom: 0px"> {{ userInfo.organName }} </el-form-item>
					</el-col>
					<el-col :span="8" class="mb10">
						<el-form-item label="保管人：" style="margin-bottom: 0px"> {{ userInfo.userName }} </el-form-item>
					</el-col>
					<el-col :span="8" class="mb10">
						<el-form-item label="保管人电话：" style="margin-bottom: 0px"> {{ userInfo.phone }} </el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="24" class="mb10">
						<el-form-item label="备注：" style="width: 100%"> {{ formInline.remark }} </el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-image
						v-for="(item, index) in formInline.imgs"
						:src="item.url"
						:key="index"
						:preview-src-list="formInline.imgs"
						style="width: 250px; height: 200px; margin-left: 10px"
					>
					</el-image>
				</el-row>
				<el-row justify="center" style="margin-top: 20px">
					<el-button type="primary" icon="el-icon-price-tag" @click="onSign(4)" align="center" size="medium">移交人签名</el-button>
					<el-button type="primary" icon="el-icon-price-tag" @click="onSign(6)" align="center" size="medium">保管人签名</el-button>
					<el-button type="primary" icon="el-icon-printer" @click="onPrint" align="center" size="medium">打印入库清单</el-button>
					<el-button type="primary" icon="el-icon-" align="center" @click="onOpen" size="medium">开柜存物</el-button>
					<el-button type="primary" icon="el-icon-" align="center" @click="onProof" size="medium">查看凭证</el-button>
				</el-row>
			</el-form>
		</el-card>
		<InInventory ref="InInventoryRef" :tableData="tableData" :formInline="formInline" />
		<ChangeInventory ref="ChangeInventoryRef" :tableData="tableData" :formInline="formInline" />
	</div>
</template>
<script lang="ts">
import { reactive, toRefs, defineComponent, ref, onMounted } from 'vue';
import { useStore } from '/@/store/index';
import { useRouter } from 'vue-router';
import { dateFormat, dateFormatTime,getEvidenceStatus } from '/@/common';
import { PostBase64Imgs } from '/@/api/common/index';
import { ElMessage } from 'element-plus';
import InInventory from '/@/views/evidence/unit/component/inInventory.vue';
import ChangeInventory from '/@/views/evidence/unit/component/changeInventory.vue';
import { openBox } from '/@/api/common/index';
import { queryImgs } from '/@/api/common/index';
import { Session } from '/@/utils/storage';
import { ElMessageBox } from 'element-plus';
import { openSign, openHardBox } from '/@/api/hardware/index';

export default defineComponent({
	name: 'EvidenceInConfirm',
	components: { InInventory,ChangeInventory },
	setup() {
		const InInventoryRef = ref();
		const ChangeInventoryRef = ref();
		const store = useStore();
		const router = useRouter();
		const state = reactive({
			formInline: store.state.customs.customs.transfereInfo,
			tableData: store.state.customs.customs.selectionEvidences,
			formInlineCase: store.state.customs.customs.formInlineCase,
			deliveryNo: store.state.customs.customs.deliveryNo,
			showinventory:store.state.customs.customs.showinventory,
			dialogVisible: false,
			dialogImageUrl: '',
			srcList: [],
			url: '',
		});
		const userInfo = Session.get('userInfo');

		if(store.state.customs.customs.showinventory)
		{
			setTimeout(() => {
				ElMessageBox.alert('请查看、打印清单',{
					callback:function(){
						onPrint();
					}
				})
			}, 500);
			
		}
		const onSign = (type) => {
			store.dispatch('hardware/getHardwareConfigByKey', 'yjpz_qianziban').then(function (cfg) {
				if (!cfg) {
					ElMessage('未配置【签字版配置】');
					return;
				}
				var _data = {
					RelevanceNo: state.deliveryNo,
					Type: type,
					url: import.meta.env.VITE_API_URL + '/common/upload/base64img',
					key: cfg.res1,
					ver: cfg.res2,
				};
				openSign(_data);
				ElMessageBox.confirm('我已经签完字').then(() => {
					queryImg(_data.Type);
				});
			});
			// cameraRef.value.openDialog();
			//Formium.external.callDevice.sysSingFunction(JSON.stringify({ id: 123, ty: type }));
		};

		onMounted(() => {
			window.clientPhotograph = (_data: any) => {
				clientPhotograph(_data);
			};

			window.openBoxResult = (_data: any) => {
				openBoxResult(_data);
			};
		});

		function openBoxResult(_data: any) {
			_data = JSON.parse(_data);
			if (_data.Success) {
				//数据传给客户端
				ElMessage.success('已打开柜子');
			}
		}

		async function clientPhotograph(_data: any) {
			_data = JSON.parse(_data);
			PostBase64Imgs({ RelevanceNo: state.deliveryNo, Type: _data.Type, base64Imgs: _data.base64Imgs }).then((res: any) => {
				if (res.success) {
					queryImg(_data.Type);
					ElMessage.success('上传成功');
				}
			});
		}

		async function queryImg(Type: any) {
			await queryImgs(state.deliveryNo, Type).then((res1: any) => {
				operateImgs(res1.data[0].url, Type);
				if (Type == 6) {
					state.formInline.saveSignImg = res1.data[0].url;
				} else {
					state.formInline.signImg = res1.data[0].url;
				}
			});
		}
		function operateImgs(url: String, type: any) {
			if (state.formInline.imgs.some((x: any) => x.type == type)) {
				state.formInline.imgs.find((x: any) => x.type == type).url = url;
			} else {
				state.formInline.imgs.push({ url: url, type: type });
			}
		}
		function onPrint() {
			
			if(state.formInline.deliveryDicType==7)
			{
				debugger
				ChangeInventoryRef.value.openDialog({
				formInlineCase: state.formInlineCase,
				transfereInfo: state.formInline,
				deliveryNo: state.deliveryNo,
				signImg: state.formInline.signImg,
				saveSignImg: state.formInline.saveSignImg,
				row: state.tableData.map((x: any) => {
					return {
						evidenceName: x.name,
						evidenceNo: x.no,
						evidenceQuntity: x.quantity,
						evidenceShape: x.shape,
						usefulLife: x.extractionDateTime,
						describe: x.describe,
						location:x.location,
						beforeLocation:x.beforeLocation,
					};
				}),
			});

			return;

		}


			InInventoryRef.value.openDialog({
				formInlineCase: state.formInlineCase,
				transfereInfo: state.formInline,
				deliveryNo: state.deliveryNo,
				signImg: state.formInline.signImg,
				saveSignImg: state.formInline.saveSignImg,
				row: state.tableData.map((x: any) => {
					return {
						evidenceName: x.name,
						evidenceNo: x.no,
						evidenceQuntity: x.quantity,
						evidenceShape: x.shape,
						usefulLife: x.extractionDateTime,
						describe: x.describe,
						location:state.formInline.location,
					};
				}),
			});
		}

		async function onOpen() {
			openBox({ tranNo: state.deliveryNo }).then((res) => {
				if (res.success) {
					var _data = res.data;
					var _isGo = true;

					for (let i = 0; i < _data.length; i++) {
						var __val = _data[i];
						if (__val.cabinetType == 1) {
							store.dispatch('hardware/getHardwareConfigByKey', 'yjpz_mijigui').then(function (cfg) {
								if (!cfg) {
									ElMessage('未配置【密集柜配置】');
									_isGo = false;
									return;
								}
								__val.key = cfg.res1;
							});
						} else if (__val.cabinetType == 2) {
							store.dispatch('hardware/getHardwareConfigByKey', 'yjpz_zhinenggui').then(function (cfg) {
								if (!cfg) {
									_isGo = false;
									ElMessage('未配置【智能柜配置】');
									return;
								}
								__val.key = cfg.res1;
							});
						}
					}
				
					setTimeout(() => {
						if (!_isGo) return;
						openHardBox(_data);
						//Formium.external.callDevice.sysCabinetFunction(JSON.stringify(_data));
						ElMessage.success('开箱成功');
					}, 10);
				} else {
					ElMessage({ type: 'warning', message: '非智能柜子，不需要开箱' });
				}
			});
		}

		function onProof() {
			router.getRoutes().find((x) => x.name == 'evidenceImg').meta.title = `凭证照片`;
			router.push({
				path: '/evidence/unit/img',
				query: {
					id: 0,
					ty: 3,
					name: state.tableData[0].name,
					no: state.deliveryNo,
				},
			});
		}
		return {
			dateFormat,
			dateFormatTime,
			openBoxResult,
			clientPhotograph,
			onSign,
			onPrint,
			onOpen,
			PostBase64Imgs,
			InInventoryRef,
			ChangeInventoryRef,
			getEvidenceStatus,
			onProof,
			userInfo,
			...toRefs(state),
		};
	},
});
</script>
